/**
 * @param {number[]} nums
 * @return {number[]}
 */
var productExceptSelf = function(nums) {
    let zeroCount = 0;
    let totalProduct = 1;
    for (const num of nums) {
        if (num === 0) {
            zeroCount++;
        } else {
            totalProduct *= num;
        }
    }
    let answer = [];
    for (let i = 0; i < nums.length; i++) {
        if (zeroCount > 1) {
            answer[i] = 0;
        } else if (zeroCount === 1) {
            answer[i] = nums[i] === 0? totalProduct : 0;
        } else {
            answer[i] = totalProduct / nums[i];
        }
    }
    return answer;

};
console.log(productExceptSelf([1,2,3,4]));
